package com.easyattendance.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.easyattendance.model.Assignment;
import com.easyattendance.model.AssignmentStudent;
import com.easyattendance.model.Student;
import com.easyattendance.services.AddAssignmentService;
import com.easyattendance.services.AddSubjectService;
import com.easyattendance.services.AssignmentSubmissionService;


@WebServlet("/GetAssignmentReportServlet")
public class GetAssignmentReportServlet extends HttpServlet{

	 protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
	    	response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			
			
			AddSubjectService subjectService=new AddSubjectService();
			AddAssignmentService assService=new AddAssignmentService();
			
			String reportType = request.getParameter("report_type");
			
			if(reportType.equals("assignmentwise")) {
			
				String classId = request.getParameter("class_selected");
				String subjectName = request.getParameter("subject_selected");
				String assignmentName = request.getParameter("assignment_selected");
				
				String subjectId=subjectService.getSubjectIdBySubjectNameForClass(subjectName,classId);
				String assignmentId=assService.getAssignmentIdByName(assignmentName,subjectId,classId);
				
				AssignmentSubmissionService assignmentService=new AssignmentSubmissionService();
				
				boolean dispatcherFlag = false;
				
				List<AssignmentStudent> stud_assList = assignmentService.listAssignmentsSubmittedByClass(classId,subjectId,assignmentId);
				 
				
				if(stud_assList.isEmpty()) {
					request.getSession().setAttribute("assignment_list_not_found", "No Records found for submitted assignments");
		        	dispatcherFlag = true;
				}
				else
					request.getSession().setAttribute("stud_assList",stud_assList);
				
				request.getSession().setAttribute("class_id",classId);
				request.getSession().setAttribute("subject_name",subjectName);
				request.getSession().setAttribute("assignment_name",assignmentName);
				
				if(dispatcherFlag) {
					/*RequestDispatcher rd = request.getRequestDispatcher("views/teacher/assignmentReports.jsp");
			    	rd.forward(request, response);*/
					
					response.sendRedirect("views/teacher/assignmentReports.jsp");
				}
				else
				{	
					/*RequestDispatcher rd = request.getRequestDispatcher("views/teacher/displayAssignmentReport.jsp");
			    	rd.forward(request, response);*/
					response.sendRedirect("views/teacher/displayAssignmentReport.jsp");
				}
			}
			else if(reportType.equals("subjectwise"))
			{
				String classId = request.getParameter("class_selected");
				String subjectName = request.getParameter("subject_selected");
				String teacherId = request.getParameter("teacher_id");
				
				String subjectId=subjectService.getSubjectIdBySubjectNameForClass(subjectName,classId);
			
				AssignmentSubmissionService assignmentService=new AssignmentSubmissionService();
				
				
				boolean dispatcherFlag = false;
			
				int no_of_assignments=assignmentService.listSubjectwiseAssignments(classId, subjectId,teacherId);
				
				List<Student> stud_List = assService.getStudentList(classId);
				List<Integer> stud_assList = new ArrayList<Integer>();
				
				for(int i=0;i<stud_List.size();i++)
				{
					 int submission_counter=0;
					 List<AssignmentStudent> assignments_submitted=assignmentService.listSubjectwiseAssignmentSubmitted(classId,subjectId,stud_List.get(i).getStud_roll_no());
					 
					 for(Iterator<AssignmentStudent> assItr=assignments_submitted.iterator();assItr.hasNext();)
					 {
						   AssignmentStudent tempAss=(AssignmentStudent)assItr.next();
						   if(tempAss.getSubmission_status().equals("Y"))
							      submission_counter++;
					 }
					 
					 
					 stud_assList.add(new Integer(submission_counter));
				 
				}
				
				if(stud_assList.size()==0) {
					request.getSession().setAttribute("assignment_list_not_found", "No Records found for submitted assignments");
		        	dispatcherFlag = true;
				}
				else
					request.getSession().setAttribute("stud_assList",stud_assList);
				
				request.getSession().setAttribute("stud_List",stud_List);
				request.getSession().setAttribute("class_id",classId);
				request.getSession().setAttribute("subject_name",subjectName);
				request.getSession().setAttribute("no_of_assignments",no_of_assignments);
				
				if(dispatcherFlag) {
					/*RequestDispatcher rd = request.getRequestDispatcher("views/teacher/assignmentReports.jsp");
			    	rd.forward(request, response);*/
					
					response.sendRedirect("views/teacher/assignmentReport.jsp");
				}
				else
				{
					/*RequestDispatcher rd = request.getRequestDispatcher("views/teacher/displayAssignmentReport.jsp");
			    	rd.forward(request, response);*/
					response.sendRedirect("views/teacher/displaySubjectwiseAssignmentReport.jsp");
				}
			}
			
	    }

		/**
		 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
		 */
		protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			
			processRequest(request, response);	
		}

		/**
		 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
		 */
		protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			processRequest(request, response);	
		}

		
}
